<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>登录界面</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <style>
        html, body {
            height: 100%;
            margin: 0;
            font-family: 'Roboto', sans-serif;
            background: #f0f8ff;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        body {
            background: linear-gradient(135deg, #f0f8ff, #e6f7ff);
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }

        .container {
            background: #ffffff;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        form {
            width: 100%;
        }

        label {
            color: #333;
            font-weight: bold;
            display: block;
            margin-bottom: 10px;
        }

        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .submit {
            background: #007bff;
            border: none;
            color: #ffffff;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .submit:hover {
            background-color: #0056b3;
        }

        a {
            text-decoration: none;
            color: #007bff;
            font-weight: bold;
            display: block;
            margin-top: 10px;
        }

        a:hover {
            text-decoration: underline;
        }

        .required {
            color: red;
            font-size: 14px;
            display: inline-block;
            margin-right: 5px;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>用户登录</h1>
    <form id="loginForm" action="/user/login" method="post">
        <label for="username"><span class="required">*</span>用户名</label>
        <input type="text" name="username" id="username" placeholder="请输入账号" required>

        <label for="pwd"><span class="required">*</span>密码</label>
        <input type="password" name="password" id="pwd" placeholder="请输入密码" required>

        <button type="submit" class="submit">登录</button>

        <a href="/register">注册</a>
    </form>
</div>
<script>
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        const formData = {
            username: document.getElementById("username").value,
            password: document.getElementById("pwd").value
        };

        fetch('/user/login', {
            method: 'POST',
            body: JSON.stringify(formData),
            headers: {
                'Content-Type': 'application/json'
            }
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok ' + response.statusText);
                }
                return response.json(); // 假设后端返回的是JSON格式的数据
            })
            .then(data => {
                console.log(data);
                if (data.code == 0) {
                    alert("登录成功！");
                    window.location.href = "/home";
                } else {
                    alert("登录失败！");
                }
            })
            .catch(error => {
                console.error('There was a problem with the fetch operation:', error);
                // 在这里处理错误，比如显示错误信息等
            });
    });
</script>
</body>

</html>
